﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AJRavindiran.Jolt.RuneScape.Models.Characters.Information;

namespace AJRavindiran.Jolt.RuneScape.Database.Configs
{
    /// <summary>
    /// Represents a set of general configurations for the global gaming environment.
    /// </summary>
    public class GeneralConfigs
    {
        #region Fields
        /// <summary>
        /// The table name to grab configurations from.
        /// </summary>
        public const string TableName = "config_general";
        /// <summary>
        /// The container for the configurations.
        /// </summary>
        private Dictionary<string, dynamic> configurations = new Dictionary<string, dynamic>();


        #region Special Configs
        /// <summary>
        /// The default spawn point for every player. When a 
        /// player dies, he/she will be spawned at this location.
        /// </summary>
        private Location spawnPoint;
        #endregion Special Configs
        #endregion Fields

        #region Properties
        /// <summary>
        /// Gets a value from the configurations container.
        /// </summary>
        /// <param name="key">The key to look for.</param>
        /// <returns>Returns a value associated with the given key.</returns>
        public dynamic this[string key]
        {
            get { return this.configurations[key]; }
        }

        /// <summary>
        /// Gets the default spawn point.
        /// </summary>
        public Location SpawnPoint
        {
            get { return this.spawnPoint; }
        }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Constructs a new general configs class.
        /// </summary>
        public GeneralConfigs()
        {
            this.Load();
        }
        #endregion Constructors

        #region Methods
        /// <summary>
        /// Reloads the configurations by clearing the 
        /// dictionary and calling <code>this.Load()</code>.
        /// </summary>
        public void Reload()
        {
            this.configurations.Clear();
            this.Load();
        }

        /// <summary>
        /// Loads the configurations and stores in a dictionary.
        /// </summary>
        public void Load()
        {
            ConfigLoader.Load(TableName, this.configurations);

            try
            {
                // Spawn point is a special config 
                // since it needs to be joined. We will 
                // do this here and store in a 'special' 
                // property which is accessed statically.
                this.spawnPoint = Location.New(
                    this.configurations["spawn_point.x"],
                    this.configurations["spawn_point.y"],
                    this.configurations["spawn_point.z"]);
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
        }
        #endregion Methods

    }
}
